package com.dearbinge.data.service.impl.rocket;

import com.baomidou.mybatisplus.plugins.Page;
import com.dearbinge.data.api.rocket.RocketService;
import com.dearbinge.data.dao.RocketMapper;
import com.dearbinge.data.dao.UserMapper;
import com.dearbinge.data.pojo.rocket.Rocket;
import com.dearbinge.data.pojo.user.User;
import com.dearbinge.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service(value = "rocketService")
public class RocketServiceImpl implements RocketService {

    @Autowired
    private RocketMapper rocketMapper;

    @Autowired
    private UserMapper userMapper;


    @Override
    public Integer update(Rocket rocket) {
        Integer dataCoce = rocketMapper.update(rocket);
        return dataCoce;
    }

    @Override
    public Integer insert(Rocket rocket) {
        Integer dataCoce = rocketMapper.insert(rocket);
        return dataCoce;
    }

    @Override
    public Map<String,Object> getByUserId(String userId, Integer rocketType) {
        Map<String,Object> rocket = rocketMapper.getByUserId(userId, rocketType);
        return rocket;
    }

    @Override
    public Integer delete(Integer id) {
        Integer dataCode = rocketMapper.delete(id);
        return dataCode;
    }

    @Override
    public PageResult getPage(Integer pageNo, Integer pageSize, String userId, Integer rocketType) {
        pageNo = (null == pageNo || pageNo < 1) ? 1 : pageNo;
        pageSize = (null == pageSize || pageSize < 0) ? 1 :pageSize;
        Page page = new Page(pageNo,pageSize);
        return null;
    }

    @Override
    public void timerNumber() {
        Integer dataCode = rocketMapper.deleteAll();
        if (dataCode > 0){
            rocketMapper.timerNumber();
        }
    }

    @Override
    public Integer toClick(String userId) {
        Map<String,Object> map = rocketMapper.getByUserId(userId,1);
        Integer update = 0;
        Integer updateUser = 0;
        User user = new User();
        Rocket rocket = new Rocket();
        rocket.setLastClickTime(new Date());
        if (Integer.parseInt(map.get("rocket_number").toString()) > 0){
                rocket.setId(Integer.parseInt(map.get("id").toString()));
                rocket.setRocketNumber(Integer.parseInt(map.get("rocket_number").toString())-1);
                update = rocketMapper.update(rocket);
        }else {
            map = rocketMapper.getByUserId(userId,0);
            if (map != null && Integer.parseInt(map.get("rocket_number").toString()) > 0) {
                rocket.setId(Integer.parseInt(map.get("id").toString()));
                rocket.setRocketNumber(Integer.parseInt(map.get("rocket_number").toString())-1);
                update = rocketMapper.update(rocket);
            }
        }
        if (update > 0){
            user.setId(Integer.parseInt(userId));
            Date date = new Date();
            user.setRank(date);
            updateUser = userMapper.update(user);
        }
        return updateUser;
    }

    @Override
    public Integer getNumber(String userId) {
        Integer countNumber = rocketMapper.getNumber(userId);
        return countNumber;
    }

    @Override
    public Integer recharge(Rocket rocket) {
        Map<String,Object> map = rocketMapper.getByUserId(rocket.getUserId(),rocket.getRocketType());
        Integer dataCoce = 0;
        if (map == null){
            dataCoce = rocketMapper.insert(rocket);
        }else{
            Integer rocketNumber = Integer.parseInt(map.get("rocket_number").toString());
            rocket.setRocketNumber(rocket.getRocketNumber()+rocketNumber);
            rocket.setId(Integer.parseInt(map.get("id").toString()));
            dataCoce = rocketMapper.update(rocket);
        }
        return dataCoce;
    }

    @Override
    public List<Rocket> getById(String userId) {
        List<Rocket> rocketList = rocketMapper.getById(userId);
        return rocketList;
    }
}
